package com.example.springbootwebrestfulcrud.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.crypto.interfaces.PBEKey;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: luzhanshi
 * @Date: 2021/1/31 - 01 - 31 - 21:46 
 * @Description: com.example.springbootwebrestfulcrud.controller
 * @version: 1.0
 */
@Controller
public class LoginController {
    /**
     * @PostMapping是restful类型注解效果等于 @RequestMapping(method = RequestMethod.POST)
     *
     */
    @PostMapping("/user/login")

    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Map map, HttpSession session){
            if (!StringUtils.isEmpty(username)&&"123456".endsWith(password)){
                session.setAttribute("loginUser",username);
//                return "dashboard";//在dashboard.html刷新页面会重复提交表单
//                登录成功后为了防止表单重复提交，可以重定向到主页
                return "redirect:/main.html";//重定向之前必须在MyMvcConfig里面配置映射
            }else {
                map.put("msg","账号密码有误！！！");
                return "login";
            }
    }
}
